package com.gams.controller;

import com.alibaba.fastjson.JSONObject;
import com.gams.common.FileUploadUtils;
import com.gams.config.ConfigConstant;
import com.gams.controller.param.VerifyParam;
import com.gams.device.readface.ReadFaceUtil;
import lombok.extern.log4j.Log4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 用户表管理
 *
 * @author hhlaohu
 * @email hh_laohu@163.com
 */
@Log4j
@RestController
@RequestMapping("/config")
public class ClientAPIController
{


    @ResponseBody
    @GetMapping
    public Object save()
    {
        JSONObject res = new JSONObject();
        res.put("client_deviceId", ConfigConstant.deviceId);
        res.put("server_ip", ConfigConstant.ip);
        res.put("server_port", ConfigConstant.port);
        res.put("project_name", ConfigConstant.project_name);
        return res;
    }

    @ResponseBody
    @PostMapping("/verify")
    public JSONObject verify(@RequestBody VerifyParam param) throws Exception
    {
        JSONObject res = new JSONObject();
        float verify = ReadFaceUtil.verify(param.getPath());
        res.put("val", verify);
        return res;
    }

    /**
     * 通用上传请求
     */
    @PostMapping("/common/upload")
    public JSONObject uploadFile(MultipartFile file) throws Exception
    {
        JSONObject res = new JSONObject();
        String filePath = "D:/deploy/upload";
        String fileName = FileUploadUtils.upload(filePath, file);
        log.info("fileName = " + fileName);
        res.put("fileName", fileName);
        res.put("code", 1);

        // 人脸识别
        float verify = 0;
        try
        {
            verify = ReadFaceUtil.verify(fileName);
        } catch (Exception e)
        {
            log.info("人脸识别发生异常");
            res.put("code", 0);
        }
        res.put("verify", verify);
        //供应商提供的人脸识别阈值
        //正常的人脸识别阈值是0.43，人证对比阈值低一些，是0.27。
        if (verify < 0.27)
        {
            res.put("code", 0);
        }
        return res;
    }

}
